<template>
	<view class="page index">
		<view class="form">
			<picker mode="selector" :range="customerList" @change="selectCustomer">
				<view class="picker">
					<common-input
					  title="所属客户" 
						placeholder="请选择" 
						v-model="details.customerName"
						:border-bottom="false"
					>
					</common-input>
				</view>
			</picker>
			<common-input
			  title="姓名" 
				placeholder="请填写联系人姓名" 
				v-model="details.name"
				:border-bottom="false"
			>
			</common-input>
			<common-input
			  title="职务" 
				placeholder="请输入联系人职务" 
				v-model="details.post"
				:border-bottom="false"
				:require="false"
			>
			</common-input>
			<picker mode="selector" :value="typeIndex" :range="typeList" @change="selectType">
				<view class="picker">
					<common-input 
					  title="类型" 
					  placeholder="请选择联系人类型" 
					  v-model="typeIndex < 0 ? '' : typeList[typeIndex]" 
						:border-bottom="false"
					  :select="true" 
					  :disabled="true" 
						:require="false"
					>
					</common-input>
				</view>
			</picker>
			<common-input
			  title="手机号码" 
				placeholder="请输入联系人手机号码" 
				v-model="details.mobile"
				:border-bottom="false"
				:type="tel"
				length='11'
			>
			</common-input>
			<common-input
			  title="电话号码" 
				placeholder="请输入联系人电话号码" 
				v-model="details.telephone"
				:border-bottom="false"
				:require="false"
			>
			</common-input>
			<common-input
			  title="邮箱地址" 
				placeholder="请输入联系人邮箱地址" 
				v-model="details.email"
				:border-bottom="false"
				:require="false"
			>
			</common-input>
			<common-input
			  title="家庭住址" 
				placeholder="请输入联系人家庭住址" 
				v-model="details.detailAddress"
				:border-bottom="false"
				:require="false"
			>
			</common-input>
			<picker mode="date" :value="birthday" :end="Date.now()" @change="selectBirthday">
					<view class="picker">
						<common-input
						  title="生日" 
							placeholder="请输入联系人生日" 
							v-model="birthday"
							:disabled="true"
							:require="false"
						>
						</common-input>
					</view>
			</picker>
			<view class="text">
				<view class="title">
					<view>
						备注
					</view>
					<!-- <view class="require"></view> -->
				</view>
				<textarea v-model="details.remark" placeholder="请输入备注消息" placeholder-class="place"></textarea>
			</view>
		</view>
		<view class="footer">
			<button @click="save">编辑</button>
		</view>
	</view>
</template>

<script>
	import commonInput from "@/components/commonInput.vue";
	import {getCustomerListApi} from "@/api/common.js";
	import {updateContactApi} from "@/api/contact_api.js";
	export default{
		data() {
			return{
				customerIndex:-1,
				customerList:['还没有可选择客户，请先创建客户'],
				customerId: [],
				typeIndex: 0,
				typeList:['关键决策人', '非关键决策人'],
				birthday:'',
				details:{}
			}
		},
		components: {
			commonInput,
		},
		onLoad(e) {
			this.details = JSON.parse(e.details)
			if(this.details.birthday != null){
				if(this.details.birthday[1] < 10){
					this.details.birthday[1] = `0${this.details.birthday[1]}`
				}
				if(this.details.birthday[2] < 10){
					this.details.birthday[2] = `0${this.details.birthday[2]}`
				}
				this.birthday = `${this.details.birthday[0]}-${this.details.birthday[1]}-${this.details.birthday[2]}`
			}
			if(this.details.master){
				this.typeIndex = 0;
			}else{
				this.typeIndex = 1
			}
		},
		onShow() {
			uni.showLoading({
				mask:true,
				title:'加载中...'
			})
			this.getCustomerList();
		},
		methods:{
			// 获取客户列表
			async getCustomerList(){
				let res = await getCustomerListApi();
				this.customerList = res.data.map((item) => item.name);
				this.customerId = res.data.map((item) => item.id);
				await uni.hideLoading();
			},
			selectCustomer(e){
				if(this.customerId.length == 0){
					uni.navigateTo({
						url:'/pages/index/customer/createCustomer'
					})
					return;
				}
				this.customerIndex = e.detail.value;
				this.details.customerId = this.customerId[this.customerIndex];
			},
			// 联系人类型
			selectType(e){
				this.typeIndex = e.detail.value;
			},
			// 联系人生日
			selectBirthday(e){
				this.birthday = e.detail.value;
			},
			save(){
				if(this.details.customerId == null){
					uni.showToast({
						icon:'none',
						title:'所属客户不能为空'
					})
					return;
				}
				if(this.details.name == ''){
					uni.showToast({
						icon:'none',
						title:'联系人姓名不能为空'
					})
					return;
				}
				if(this.details.mobile == ''){
					uni.showToast({
						icon:'none',
						title:'手机号不能为空'
					})
					return;
				}
				uni.showLoading({
					mask:true,
					title:'加载中...'
				})
				updateContactApi({
					id: this.details.id,
					customerId: this.details.customerId,
					name: this.details.name,
					post: this.details.post,
					master: this.typeIndex == 0 ? true : false,
					mobile: this.details.mobile,
					telephone: this.details.telephone,
					email: this.details.email,
					detailAddress: this.details.detailAddress,
					birthday:this.birthday,
					remark: this.details.remark
				}).then(() => {
					uni.hideLoading();
					uni.showToast({
						icon:"success",
						title:'编辑成功'
					});
					setTimeout(() => {
						uni.navigateBack()
					},1500)
				}).catch(() => {
					uni.hideLoading();
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/common/style/customicons.scss";
	.index{
		padding: 24rpx 0;
		@include flex-c;
		.form{
			flex: 1 0 0;
			overflow: auto;
			.text{
				border-bottom: 4rpx #e5e5e5 solid;
				padding: 12rpx 30rpx;
				.title{
					font-size: 28rpx;
					color: #101010;
					padding: 0 0 16rpx;
					display: flex;
					align-items: center;
					.require{
					  height: 28rpx;
					  width: 6rpx;
					  background-color: #c24135;
					  margin-left: 10rpx;
				  }
				}
				textarea{
					font-size: 28rpx;
					color: #909090;
					height: 125rpx;
				}
				.place{
					font-size: 28rpx;
					color: #909090;
				}
			}
			&::-webkit-scrollbar{
				display: none;
			}
		}
		.footer{
			flex: 0 0 auto;
			padding: 24rpx 130rpx;
			button{
				@include button;
				background-color: #198AFA;
				color: #fff;
				height: 60rpx;
				line-height: 60rpx;
				font-size: 28rpx;
				width: 100%;
			}
		}
	}
</style>